<template>
  <div></div>
</template>
<script>
  import {translatePrice} from '@/utils/TranslatePriceC/index.js'
  import {mapState, mapGetters} from 'vuex'
  import {queryProcessList} from '../../utils/ReturnAxios'

  export default {
    data() {
      return {
        buttonType: 2,
        city: "", //产地
        enterpriseName: "宁家", // 商家名称
        eachPrice: 0,
        featureLabelList: null, // 类名查询表
        featureClass: '', //特色标签css类名
        featureLabel: 0, //特色标签
        featureName: '',
        goodsCode: "", //产品分类
        goodsId: "", //产品ID
        goodsName: "", //产品名称
        goodsPrice: "", //产品价格
        goodsUnit: "", //产品单位
        goodsModel: "", // 商品型号
        goodsType: 0, //售价
        goodsDeliveryType: '', //商品类型大件常规
        height: 0, //外形高度
        isShow: true, // 是否展示
        inActivity: 0,
        regularTypeLabel: '特价', // 优惠类型标签
        labellistFir: '', //标签1
        labellistSec: '', //标签2
        labellistThir: '', //标签3
        labellistFor: '', //标签4
        mainPic: '', //主图缩略图
        normalPrice: "1.00", //原价
        shopName: "", //商店名称
        saleqty: "", //库存
        subTitle: '', // 二级标题
        showWsPrice: 0, // 展示用的价格
        showNormalPrice: 0,
        showUnit: '', //展示用的规格
        regularLabel: [],
        titleList: {
          color: 'black',
          fontSize: '0.7rem',
          fonsWeight: '400',
        },
        labelStyle: {},
        titleStyle: {},
        subTitleStyle: {},
        buttonStyle: {
          backgroundColor: '',
          color: '#EF5423'
        },
      }
    },
    props: {
      comInfor: {
        type: Object,
        default: () => ({}),
      },
      styleConfig: {
        itemStyle: {
          'font-size': ''
        },
        goodsPriceStyle: {},
        normalPriceStyle: {},
        labelStyle: {},
        titleStyle: {},
        subTitleStyle: {},
        buttonStyle: {
          backgroundColor: '',
          color: '#EF5423'
        },

      },
      buttonActive: {
        default: null

      },
      isOpacity: {
        type: Boolean,
        default: false
      },
      wrpperHeight: {
        type: Number,
        default: 0
      },
      wrpperTop: {
        type: Number,
        default: 0
      },
      goMoreFn: {
        default: null
      },
      isLastItem: {
        type: Boolean,
        default: false
      }
    },
    methods: {

      // 获取加工信息列表
      async queryProcessList() {
        var goodsId = this.goodsId
        var processList = this.processListData[goodsId]

        // if (processList) {
        //   return processList
        // }
        var res = await
          queryProcessList(goodsId)
        res = res.data
        console.log('res ______', res)
        if (res.returnCode) {
          processList = res.data[0].processlist
        } else {
          processList = []
        }
        this.$store.commit('cjy/ADD_PROCESS_LIST_DATA', {[goodsId]: processList})
        return processList
      },

      // 加入购物车
      async addButton() {
        var processList = await
          this.queryProcessList()
        processList.length ? window.PSEvent.trigger('addCartForSpec', {
          commodityInfo: this.comInfor,
          processList
        }) : this.add()
      },

      //  直接加入购物车
      add() {
        var updateCart = this.$refs.updateCart
        updateCart && updateCart.addToCart()
      },

      translatePrice: translatePrice,
      goMore() {
        this.goMoreFn && this.goMoreFn()
      },
      changePrice(data) {
        return parseFloat(data).toFixed(2)
      },
      initData() {
        var comInfor = this.comInfor
        this.goodsCode = comInfor.goodsCode //产品分类
        this.enterpriseName = comInfor.enterpriseName // 商家名称
        this.goodsId = parseInt(comInfor.goodsId) //产品ID
        this.goodsName = comInfor.goodsName //产品名称
        this.goodsPrice = comInfor.goodsPrice //产品价格
        this.goodsUnit = comInfor.goodsUnit //产品单位
        this.city = comInfor.city //产地
        this.shopName = comInfor.shopName //商店名称
        this.saleqty = comInfor.saleqty //库存
        this.goodsModel = comInfor.goodsModel // 商品型号
        this.goodsType = comInfor.goodsType //售价
        this.normalPrice = comInfor.normalPrice //原价
        this.mainPic = comInfor.mainPic //主图缩略图
        this.subTitle = comInfor.subTitle //二级标题
        this.showUnit = comInfor.showUnit
        this.showWsPrice = comInfor.showWsPrice
        this.inActivity = comInfor.inActivity
        this.goodsDeliveryType = comInfor.goodsDeliveryType
        this.labellistFir = comInfor.labellistFir
        this.labellistSec = comInfor.labellistSec
        this.labellistThir = comInfor.labellistThir
        this.eachPrice = comInfor.eachPrice
        this.labellistFor = comInfor.labellistFor
        this.regularTypeLabel = comInfor.regularTypeLabel
        this.showNormalPrice = comInfor.showNormalPrice
        this.lastStockCountNum = comInfor.lastStockCountNum || 0
        this.regularLabel = comInfor.regularLabel
        if (this.lastStockCountNum <= 0) {
          this.buttonStyle.color = 'gray'
        }
      },
      gotoDetail() {
        this.$router.push({
          path: 'detail',
          query: {
            code: this.goodsCode
          }
        })
      },

      changStyle() {
        if (!this.isOpacity) {
          return true
        }
        var obj = this.$refs.listItem
        var itemTop = obj ? obj.getBoundingClientRect().top - this.wrpperTop : 0
        var height = this.height
        if (itemTop < this.wrpperHeight - 3 && itemTop + height > 0) {
          this.isShow = true
        } else {
          this.isShow = false
        }
      },

      //  点击加入购物车 调用的事件
      cartButtonOnClick() {
        if(this.lastStockCountNum > 0) {
          if(this.isShare) {
	      	   this.$router.push({
							name: "MiddleWare",
						})
              return
          }
          if (this.buttonActive) {
            this.buttonActive()
          } else {
            this.addButton()
          }
        }  
      }

    },
    computed: {
      ...mapState('cjy', ['processListData']),
      pagePosY() {
        var post = this.$store.state.cjy.pagePosY
        return post
      },
      isShare() {
				let share = this.$route.query.share;
				if(share) {
					return true;
				} else {
					return false
				}
			}
    },
    watch: {
      pagePosY(data) {
        this.changStyle()
      }
    },
    created() {
      this.initData()
    },
    mounted() {

      this.$nextTick(() => {
        var obj = this.$refs.listItem
        this.height = obj ? obj.offsetHeight : 0
        this.changStyle()
      })
    },
    updated() {
      this.changStyle()
    },
  }
</script>

<style>
  .cjy-page-image-box-img {
    position: absolute;
    width: 100%;
    height: 100%;
  }
</style>
